package com.ruoyi.generator.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.generator.domain.FromBuildRuleConfig;
import com.ruoyi.generator.service.IFromBuildConfigService;
import com.ruoyi.generator.service.IFromBuildRuleConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 单设计_正则校验规则配置Controller
 *
 * @author XL
 * @date 2022-08-05 12:18:44
 */
@RestController
@RequestMapping("/tool/gen/FromBuildRuleConfig")
public class FromBuildRuleConfigController extends BaseController
{
    @Autowired
    private IFromBuildRuleConfigService fromBuildRuleConfigService;

    @Autowired
    private IFromBuildConfigService fromBuildConfigService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取字段信息
     * @return
     */
    @GetMapping("/init")
    public AjaxResult getFieldInfo(){
        return fromBuildConfigService.getColumnConfigByTableName("sys_from_build_rule_config");
    }

    /**
     * 查询单设计_正则校验规则配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildRuleConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(FromBuildRuleConfig fromBuildRuleConfig){
        try{
            startPage();
            List<FromBuildRuleConfig> list = fromBuildRuleConfigService.selectFromBuildRuleConfigList(fromBuildRuleConfig);
            return getDataTable(list);
        }catch (Exception e){
            logger.error("FromBuildRuleConfigController接口list异常",e);
            return null;
        }
    }

    /**
     * 导出单设计_正则校验规则配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildRuleConfig:export')")
    @Log(tableName = "sys_from_build_rule_config", title = "单设计_正则校验规则配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FromBuildRuleConfig fromBuildRuleConfig){
        try{
            List<FromBuildRuleConfig> list = fromBuildRuleConfigService.selectFromBuildRuleConfigList(fromBuildRuleConfig);
            ExcelUtil<FromBuildRuleConfig> util = new ExcelUtil<FromBuildRuleConfig>(FromBuildRuleConfig.class);
            return util.exportExcel(list, "FromBuildRuleConfig");
        }catch (Exception e){
            logger.error("FromBuildRuleConfigController接口export异常",e);
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 导入单设计_正则校验规则配置列表
     */

    /**
     * 获取单设计_正则校验规则配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildRuleConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        try{
            return AjaxResult.success(fromBuildRuleConfigService.selectFromBuildRuleConfigById(id));
        }catch (Exception e){
            logger.error("FromBuildRuleConfigController接口getInfo异常",e);
            return AjaxResult.error("获取异常");
        }
    }

    /**
     * 新增单设计_正则校验规则配置
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildRuleConfig:add')")
    @Log(tableName = "sys_from_build_rule_config", title = "单设计_正则校验规则配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FromBuildRuleConfig fromBuildRuleConfig){
        try{
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            fromBuildRuleConfig.setCreateBy(loginUser.getUsername());
            fromBuildRuleConfig.setCreateTime(new Date());
            return toAjax(fromBuildRuleConfigService.insertFromBuildRuleConfig(fromBuildRuleConfig));
        }catch (Exception e){
            logger.error("FromBuildRuleConfigController接口add异常",e);
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 修改单设计_正则校验规则配置
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildRuleConfig:edit')")
    @Log(tableName = "sys_from_build_rule_config", title = "单设计_正则校验规则配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FromBuildRuleConfig fromBuildRuleConfig){
        try{
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            fromBuildRuleConfig.setUpdateBy(loginUser.getUsername());
            fromBuildRuleConfig.setUpdateTime(new Date());
            return toAjax(fromBuildRuleConfigService.updateFromBuildRuleConfig(fromBuildRuleConfig));
        }catch (Exception e){
            logger.error("FromBuildRuleConfigController接口edit异常",e);
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 删除单设计_正则校验规则配置
     */
    @PreAuthorize("@ss.hasPermi('system:FromBuildRuleConfig:remove')")
    @Log(tableName = "sys_from_build_rule_config", title = "单设计_正则校验规则配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        try{
            return toAjax(fromBuildRuleConfigService.deleteFromBuildRuleConfigByIds(ids));
        }catch (Exception e){
            logger.error("FromBuildRuleConfigController接口remove异常",e);
            return AjaxResult.error("操作失败");
        }
    }

    @Log(tableName = "sys_from_build_rule_config", title = "单设计_正则校验规则配置", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:FromBuildRuleConfig:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FromBuildRuleConfig> util = new ExcelUtil<FromBuildRuleConfig>(FromBuildRuleConfig.class);
        List<FromBuildRuleConfig> dataList = util.importExcel(file.getInputStream());
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        String operName = loginUser.getUsername();
        int successCount=0;
        for(FromBuildRuleConfig item : dataList){
            successCount+=fromBuildRuleConfigService.insertFromBuildRuleConfig(item);
        }
        String result= StringUtils.format("导入成功条数:{}，失败条数：{}",successCount,(dataList.size()-successCount));
        return AjaxResult.success(result);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<FromBuildRuleConfig> util = new ExcelUtil<FromBuildRuleConfig>(FromBuildRuleConfig.class);
        return util.importTemplateExcel("单设计_正则校验规则配置");
    }

}
